package com.itheima.health.service.impl;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.dao.UserOperateDao;
import com.itheima.health.dto.MemberParam;
import com.itheima.health.dto.OrderParam;
import com.itheima.health.dto.OrderSettingParam;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.UserOperateService;
import com.itheima.health.util.AliyunSmsUtil;
import com.itheima.health.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class UserOperateServicelmpl implements UserOperateService {
    @Autowired
    private UserOperateDao userOperateDao;
    @Autowired
    private OrderSettingDao orderSettingDao;

    /**
     * 获取验证码
     */
    @Override
    public void send(MemberParam memberParam) {
        //随机4位数验证码，我这里使用的hutool随机数工具类进行生成
        String code = RandomUtil.randomNumbers(4);
        //获取手机号
        String telephone = memberParam.getTelephone();
        //封装工具类所需参数
        Map<String,String> map = new HashMap<>();
        map.put("phoneNumbers",telephone);
        map.put("signName","王宣栋的博客");
        map.put("templateCode","SMS_460755722");
        //调用工具类完成短信发送
        String success = AliyunSmsUtil.sendSms(map,code);
        //验证短信发送是否成功
        if(success !=null){
            System.out.println("短信发送成功");
        }else{
            System.out.println("短信发送失败");
        }
        //添加数据库
        memberParam.setValidateCode(success);
        memberParam.setValidateCodeTime(LocalDateTime.now());
        userOperateDao.addValidateCode(memberParam);
    }

    /**
     * C端
     * 用户登录
     */
    @Override
    public void smsLogin(MemberParam memberParam) {
        //如果手机号或者验证码为null，则返回异常信息
        if (memberParam.getValidateCode() == null || memberParam.getTelephone() == null){
            throw new BusinessRuntimeException(MessageConst.TELEPHONE_VALIDATECODE_NOTNULL);
        }
        //查询数据库中验证码是否一致
        //如果数据库中没有验证码，则提示请获取新的验证码
        MemberParam newMemberParam = userOperateDao.getBytelephone(memberParam);
        if (newMemberParam.getValidateCode() == null){
            throw new BusinessRuntimeException(MessageConst.VADAFADFA_AFAAAAF);
        }
        //如果数据库中存储的验证码与当前验证码不一致，则返回登录失败
        if (! newMemberParam.getValidateCode().equals(memberParam.getValidateCode())){
            throw new BusinessRuntimeException(MessageConst.VALIDATECODE_ERROR);
        }
    }

    @Override
    public List<Setmeal> getSetmeal() {
        List<Setmeal> setmealList = userOperateDao.selectAll();
        //根据套餐id查询套餐对应的检查组
        for (Setmeal setmeal : setmealList) {
            List<CheckGroup> checkGroupList = userOperateDao.getCheckBySetmeal(setmeal);
            setmeal.setCheckGroups(checkGroupList);
        }
        return setmealList;
    }

    /**
     * 根据套餐id查询详情
     */
    @Override
    public Setmeal getCheckGroupBySetmealId(Integer id) {
        //根据套餐id查询套餐详情
        Setmeal setmeal = userOperateDao.getSetmeal(id);
        //根据套餐id查询套餐对应的检查组
        List<CheckGroup> checkGroupList = userOperateDao.getCheckBySetmeal(setmeal);
        setmeal.setCheckGroups(checkGroupList);
        //根据检查组id查询检查项
        for (CheckGroup checkGroup : checkGroupList) {
            List<CheckItem> checkItems = userOperateDao.getitemByCheckGroup(checkGroup);
            checkGroup.setCheckItems(checkItems);
        }
        return setmeal;
    }
    /*
     * 用户预约
     * */
    @Override
    public void submit(OrderSubmitParam orderSubmitParam) {

        OrderSettingParam orderSettingParam = userOperateDao.query(orderSubmitParam.getOrderDate());
        if (orderSettingParam.getNumber() > orderSettingParam.getReservations()){

            orderSettingDao.update(orderSubmitParam);

        }else {
            throw new BusinessRuntimeException(MessageConst.ORDER_FULL);
        }
        //添加预约
        orderSubmitParam.setRegTime(LocalDateTime.now());
        userOperateDao.submit(orderSubmitParam);


        //添加会员
        Integer id = orderSubmitParam.getId();
        Order order = new Order();
        order.setMemberId(id);
        order.setOrderDate(orderSubmitParam.getOrderDate());
        order.setOrderType(Order.ORDERTYPE_WEIXIN);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        order.setSetmealId(orderSubmitParam.getSetmealId());
        userOperateDao.increase(order);

    }

    @Override
    public MemberParam queryCode(String telephone) {
        MemberParam memberParam = userOperateDao.queryCode(telephone);
        return memberParam;
    }

    @Override
    public OrderParam findOrderById(Integer id) {
        OrderParam order = userOperateDao.findOrderByid(id);
        return order;
    }
}
